#include "stdafx.h"
#include "AI_Manager.h"

AI_Manager::AI_Manager(Ogre::SceneManager* sceneMgr, OgreBulletDynamics::DynamicsWorld* world, OgreOggSound::OgreOggSoundManager *soundManager,
					   std::vector<Vector3> agentStartPos, std::vector<Waypoint>* waypoints, std::vector<TimingGate> *gates, Real *raceTime)
{
	mSceneMgr = sceneMgr;
	mWorld = world;
	mSoundManager = soundManager;
	currWaypoints = waypoints;
	mEngineGainChanged = false;
	mTrackPath = new AI_TrackPath(currWaypoints);
	soundStarted = false;
	for(int i = 0; i < agentStartPos.size(); i++)
	{
		mAgents.push_back(new AI_Agent(i, mTrackPath, mSceneMgr, mWorld, agentStartPos[i], gates, raceTime));
		mSoundManager->createSound("Engine_Rev" + StringConverter::toString(i), "engine_rev.ogg", false, true, true);
		mSoundManager->getSound("Engine_Rev" + StringConverter::toString(i))->setVolume(0.25f);
		mSoundManager->getSound("Engine_Rev" + StringConverter::toString(i))->setRolloffFactor(0.75f);
		mSoundManager->getSound("Engine_Rev" + StringConverter::toString(i))->setReferenceDistance(10.0f);
		mSoundManager->getSound("Engine_Rev" + StringConverter::toString(i))->setPitch(0.65f);
		mAgents[i]->attachSoundtoEngine("Engine_Rev" + StringConverter::toString(i), mSoundManager);
	}
}


AI_Manager::~AI_Manager(void)
{
	delete mTrackPath;
	for (int i = 0; i < mAgents.size(); i++)
	{
		delete mAgents[i];
	}
	mAgents.clear();
}

void AI_Manager::update(Ogre::Real elapsedTime, GameState state)
{
	if(state == COUNTDOWN)
	{
		if(!soundStarted)
		{
			startSound();
		}
	}

	if(state == RACING)
	{
		if(!mEngineGainChanged)
		{
			setRacingVolume();
		}
	}

	//loop through agents and tell them what to do
	for(int i = 0; i < mAgents.size(); i++)
	{
		if(state == RACING)
		{
			mSoundManager->getSound("Engine_Rev" + StringConverter::toString(i))->setPitch(0.65f + (mAgents[i]->getVelocity() / 115)*0.85f);
		}
		mAgents[i]->update(elapsedTime, state);
	}
}

int AI_Manager::getAgentWaypoint(int index)
{
	return mAgents[index]->currWaypointIndex;
}

Real AI_Manager::getAgentVelocity(int index)
{
	return mAgents[index]->getVelocity();
}

int AI_Manager::calculatePlayerPosition(Player *p)
{
	int position = mAgents.size() + 1;
	int pLap = p->getCurrentLap();
	int pGate = p->getLastTimingGate();
	Real pGateTime = p->getLastTimingGateTime();
	for(int i = 0; i < mAgents.size(); i++)
	{
		if(pLap > mAgents[i]->getCurrentLap())
		{
			position--;
		}
		else if(pLap == mAgents[i]->getCurrentLap())
		{
			if(pGate > mAgents[i]->getLastTimingGate())
			{
				position--;
			}
			else if(pGate == mAgents[i]->getLastTimingGate())
			{
				if(pGateTime <= mAgents[i]->getLastTimingGateTime())
				{
					position--;
				}
			}
		}
	}
	return position;
}

int AI_Manager::numAgents()
{
	return mAgents.size();
}

void AI_Manager::startSound()
{
	for(int i = 0; i < mAgents.size(); i++)
	{
		mSoundManager->getSound("Engine_Rev" + StringConverter::toString(i))->play();
	}
	soundStarted = true;
}

void AI_Manager::setRacingVolume()
{
	for(int i = 0; i < mAgents.size(); i++)
	{
		mSoundManager->getSound("Engine_Rev" + StringConverter::toString(i))->setVolume(0.85f);
	}
	mEngineGainChanged = true;
}